package com.jtx.miaoah2.Cache;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * @author 季台星
 * @Date 2023 07 27 09 40
 */
@Component
public class HotCache {
    @Autowired
    private StringRedisTemplate redisTemplate;

    public boolean addHotKet(String keyType,String keyId){
        return redisTemplate.opsForZSet().addIfAbsent(keyType, keyId, 0);
    }

    public void addHotScore(String keyType,String keyId,Integer delta){
        redisTemplate.opsForZSet().incrementScore(keyType, keyId, delta);
    }

    /**
     * 根据热度返回所有热点key
     * @return
     */
    public Set<String> getByHotScore(String keyType){
        return redisTemplate.opsForZSet().reverseRange(keyType, 0, Integer.MAX_VALUE);
    }
}
